package booroo.swing;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import java.awt.Dimension;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;

import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;

import booroo.dao.MemberDAO;
import booroo.model.MemberVO;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class ManageMember extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTable table;
	private MemberDAO mdao = new MemberDAO();
	private DefaultTableModel model = new DefaultTableModel();


	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			ManageMember dialog = new ManageMember();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public ManageMember() {
		setBounds(100, 100, 640, 480);
		setLocationRelativeTo(null);
		setTitle("회원관리");
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JPanel listPane = new JPanel();
			listPane.setPreferredSize(new Dimension(640, 360));
			contentPanel.add(listPane, BorderLayout.NORTH);
			{
				JScrollPane scrollPane = new JScrollPane();
				scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
				scrollPane.setPreferredSize(new Dimension(600, 350));
				listPane.add(scrollPane);
				{
					ArrayList<MemberVO> list = mdao.selectMember();
					
					model = new DefaultTableModel(){
						public boolean isCellEditable(int row, int col) {
							return false;
						}
							
					};
					model.setColumnIdentifiers(new String[]{"회원번호","이름","아이디","등록일"});
					for(MemberVO m : list){
						model.addRow(new String[]{m.getMno(),m.getName(),m.getUserid(),m.getRegdate()});
						}
										
					table = new JTable();
					table.addMouseListener(new MouseAdapter() {
						@Override
						public void mouseClicked(MouseEvent e) {
							JTable t = (JTable)e.getSource();
							if(e.getClickCount() == 2){
								
							}
						}
					});
					table.setModel(model);
					scrollPane.setViewportView(table);
				}
			}
		}
		{
			JPanel btnPane = new JPanel();
			btnPane.setPreferredSize(new Dimension(640, 30));
			contentPanel.add(btnPane, BorderLayout.SOUTH);
			btnPane.setLayout(new GridLayout(1, 0, 0, 0));
			
			JButton addBtn = new JButton("회원추가");
			btnPane.add(addBtn);
			
			JButton modBtn = new JButton("회원수정");
			modBtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int r = table.getSelectedRow();
					if(r == -1)
						JOptionPane.showMessageDialog(null, "수정할 데이터를 선택하세요!");
					else{
					String mno = table.getValueAt(r, 0).toString();
					
						UpdateMember upm = new UpdateMember(mno,r,table);
						// 회원정보 수정을 위해 객체를 생성
						// 단, UpdateMember 의 생성자에 수정할 회원 번호를 매개변수로 전달
						// 또한 회원정보 수정후 JTable의 model을 변경해야 하므로
						// 수정해야 할 행row과 JTable객체도 생성자 매개변수로 선언
						upm.setVisible(true);
											}
					
					
				}
			});
			btnPane.add(modBtn);
			{
				JButton delBtn = new JButton("회원삭제");
				delBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						int r = table.getSelectedRow();
						if(r == -1){  // 행을 선택하지 않으면 -1
							JOptionPane.showMessageDialog(null, "삭제할 행을 선택하세요!");
							return;	// 코드 실행 중단!
						}
						int choise = JOptionPane.showConfirmDialog(null, "정말로 삭제하시겠습니까?");
						if(choise == JOptionPane.OK_OPTION){
						
						String mno = table.getValueAt(r, 0).toString();
						if(mdao.deleteMember(mno)>0)
							JOptionPane.showMessageDialog(null, "성공적으로 삭제됨!");
						
						// 선택한 JTable의 행 삭제하고 JTable을 다시 출력
						model.removeRow(r);
						table.repaint();
						}
					}
				});
				btnPane.add(delBtn);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
}
